<?php
define( '_JEXEC', 1 );
defined('_JEXEC') or die('Restricted access');
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE',dirname(__FILE__).DS."..".DS."..");
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'loader.php' );
ini_set('memory_limit', '1024M'); 

jimport( 'joomla.application.cli' );
jimport('joomla.database.database');
jimport( 'joomla.database.table' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
JPluginHelper::importPlugin('system');

$db =& JFactory::getDBO();

$user =& JFactory::getUser();
$user_id = $user->get('id');
	
	
	if (isset($_POST["PHPSESSID"])) {
		session_id($_POST["PHPSESSID"]);
	}
	session_start();

	// The Demos don't save files
	
	//recursively hash file name uptill slot is available
function file_uploader_upload_hashed_name($File, $path){

	$exist_already = false;
	$hash_value = 'test';

	do{
		$hash_value = file_uploader_get_file_id_key($File);
		$File['name']['file'] = $hash_value ;
	}while(
		$exist_already = file_exists($path.$hash_value.".".file_uploader_get_filetype($File)	)
	);

	$uploadfile = 	$path.
						$hash_value.".".
						file_uploader_get_filetype($File);	

	if( move_uploaded_file($File['tmp_name']['file'], 'gary123.jpg') ){

		return $hash_value.".".file_uploader_get_filetype($File);
	}	
	else{

		return false;
	}
}

//recursively hash file name uptill slot is available for SWF Upload
function file_uploader_upload_hashed_name_swf($File, $path){

	$exist_already = false;
	$hash_value = 'test';
	//print_r($File);
	do{

		//echo $File['name']."\r\n";
		$hash_value = md5($File['name']);			
		$File['name'] = $hash_value.".".file_uploader_swf_type_finder($File['name']);
		//echo "checking ".$path.$hash_value.".".file_uploader_swf_type_finder($File['name'])."\r\n";		
	}while(
		$exist_already = file_exists($path.$hash_value.".".file_uploader_swf_type_finder($File['name'])	)
	);

	$uploadfile = 	$path.
						$hash_value.".".
						file_uploader_swf_type_finder($File['name']);	
	/*
	if( move_uploaded_file($File['tmp_name']['file'], $uploadfile) ){
		echo "Successful upload";
		return $hash_value.".".file_uploader_get_filetype($File);
	}
	*/
	//echo "uploading to $uploadfile \r\n";

	if( move_uploaded_file($File['tmp_name'], $uploadfile) ){
		//echo "Successful upload";
		return $hash_value.".".file_uploader_swf_type_finder($File['name']);
	}	
	else{
		//echo "Failed upload";	
		return false;
	}
}

function file_uploader_swf_type_finder($filename){
	$filename = explode(".",$filename);
	return $filename[count($filename)-1];
}

//uploads the file as per normal
function file_uploader_upload_normal($File,  $path){

	if( move_uploaded_file($File['tmp_name'], $path.$File['name']) ){
		return true;
	}
	else{
		return false;
	}
}

//deletes a file
function file_uploader_deleteFile($File, $path){
	unlink($path.$File);
}

function file_uploader_get_filetype($File){
	$type = $File["type"]['file'];			
	$type = strstr ( $type, '/');
	$type = substr($type, 1); 

	return $type;
}

function file_uploader_get_file_id_key($File){

	$ID = md5($File['name']['file']);		

	return $ID;
}

$max_uploads = 2;

$query = 'SELECT count(*) FROM '.$db->nameQuote('#__putmovies_rating').' WHERE '.$db->nameQuote('user_id').' = '.$user_id.' AND DATE('.$db->nameQuote('date').') = DATE(NOW())';
$db->setQuery($query);
$dbResults = $db->loadResult();

if($dbResults < $max_uploads) {
		
	//ensures only xml files are uploaded.
	if(strtolower(substr($_FILES['Filedata']['name'], (strlen($_FILES['Filedata']['name']))-4, 4))== '.php'){
		echo "Sorry for security purposes uploading of PHP files are allowed ";
		exit();
	}
	
	else if(is_file('../../files/'.$_FILES['Filedata']['name'])){
		echo "Sorry only XML Workfow file with Name [".$_FILES['Filedata']['name']."] already exist";
		exit();
	}
	
	$md5_file_name = md5($_FILES['Filedata']['name'].'_'.date("d.m.Y H:i:s.u").'_'.rand(0,99999));
	
	$_FILES['Filedata']['name'] = $md5_file_name.'_'.$_FILES['Filedata']['name'];
	$file_name = file_uploader_upload_normal($_FILES['Filedata'],  '../../files/');		
	
	echo $_FILES['Filedata']['name'];
}
else
{
	echo "Wykorzystano dzisiejszy limit upload'ow!";
}
	
exit();
?>